package com.chinaoct.siku.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;
import java.util.Base64;

public class DESUtil {

    private final static Base64.Decoder decoder = Base64.getDecoder();
    private final static Base64.Encoder encoder = Base64.getEncoder();
    private final static String password="91528384052687880010931324570753332531618984426334783573298773549468758875018579537757772163088447848736934944734060744466230061464119604557412243340459458924702753686062729012";


   public  static  String encrypt(String value){
       byte[] datasource=value.getBytes();
       byte[] result =encrypt(datasource);
       return encode(result);

   }
    /**
     * 加密
     * @param datasource byte[]
     * @param password String
     * @return byte[]
     */
    public static  byte[] encrypt(byte[] datasource, String password) {
        try{
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            //创建一个密匙工厂，然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在，获取数据并加密
            //正式执行加密操作
            return cipher.doFinal(datasource);
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }
    public static  byte[] encrypt(byte[] datasource) {
        return encrypt(datasource,password);
    }


    /**
     * 将base64转换为string
     * @param datasource
     * @return
     */
    public static String encode(byte[] datasource){
        return  encoder.encodeToString(datasource);
    }
    public static byte[] decode(String encodedText) throws UnsupportedEncodingException {
        return decoder.decode(encodedText);
    }
    /**
     * 解密
     * @param src byte[]
     * @param password String
     * @return byte[]
     * @throws Exception
     */
    public static byte[] decrypt(byte[] src, String password) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
        DESKeySpec desKey = new DESKeySpec(password.getBytes());
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }

    public static byte[] decrypt(byte[] src) throws Exception {
        return  decrypt(src,password);
    }

    public static String decrypt (String value)throws Exception {
        byte[] codes= decode(value);
        byte[] decryResult = decrypt(codes);
        return new String(decryResult);
    }

    //测试
    public static void main(String args[]) {
        //待加密内容
        String str = "测试内容";
        //密码，长度要是8的倍数
        byte[] result = DESUtil.encrypt(str.getBytes(),password);

        String code=encode(result);
        System.out.println("加密后："+new String(code));
        //直接将如上内容解密
        try {
            byte[] codes= decode(code);
            byte[] decryResult = DESUtil.decrypt(codes, password);
            System.out.println("解密后："+new String(decryResult));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
